import { listService } from './List.service';
import {ListConstants} from './constants'
export const ListActions = {
    getList,
    getOne,
    clearList

};

function getList(url) {
    return dispatch => {
 
        dispatch(request());
        listService.getList(url)
            .then(
                list => dispatch(success(list)),
                error => dispatch(failure(error.detail))
            );
    };
    function request() { return { type: ListConstants.GETALL_REQUEST } }
    function success(list) { return { type: ListConstants.GETALL_SUCCESS, list } }
    function failure(error) { return { type: ListConstants.GETALL_FAILURE, error } }
}
function clearList() {
return { type: ListConstants.CLEAR_LIST } 
 
}
function getOne(url,refresh=false) {
    return dispatch => {

        dispatch(request());
        listService.getOne(url,refresh)
            .then(
                one => dispatch(success(one)),
                error => dispatch(failure(error.detail))
            );
    };

    function request() { return { type: ListConstants.GETONE_REQUEST } }
    function success(one) { return { type: ListConstants.GETONE_SUCCESS, one } }
    function failure(error) { return { type: ListConstants.GETONE_FAILURE, error } }
}
